package cc.rengu.igas.bomp.common.dao.impl;

import cc.rengu.igas.bomp.common.dao.ManualReconErrTxnMapper;
import cc.rengu.igas.bomp.common.entity.ReconErrTxn;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 第三方差错交易表
 * Created by xubh on 2020/6/3
 */
public class ManualReconErrTxnMapperImpl implements ManualReconErrTxnMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.CSMC_POOL_NAME);

    @Override
    public ReconErrTxn selectReconErrTxnByprimaryKey(String primaryKey) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectReconErrTxnByprimaryKey");
        ReconErrTxn reconErrTxn = new ReconErrTxn();
        reconErrTxn.setPrimaryKey(primaryKey);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, reconErrTxn, reconErrTxn);
        if (0 != iReturnCode) {
            rglog.error("获取对账不平信息失败,primaryKey:<{}>", primaryKey);
            return null;
        }
        return reconErrTxn;
    }

    @Override
    public int updateReconErrTxnByprimaryKey(ReconErrTxn reconErrTxn) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateReconErrTxnByprimaryKey");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, reconErrTxn, null);
        if (0 != iReturnCode) {
            rglog.error("更新差错调账流水失败,instId:<{}>,primaryKey:<{}>,iReturnCode:<{}>", reconErrTxn.getInstId(), reconErrTxn.getPrimaryKey(), iReturnCode);
        }
        return iReturnCode;
    }

    @Override
    public int updateReconErrTxnAdjustStatusByprimaryKey(ReconErrTxn reconErrTxn) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "updateReconErrTxnAdjustStatusByprimaryKey");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, reconErrTxn, null);
        if (0 != iReturnCode) {
            rglog.error("更新差错调账流水失败,instId:<{}>,primaryKey:<{}>,iReturnCode:<{}>", reconErrTxn.getInstId(), reconErrTxn.getPrimaryKey(), iReturnCode);
        }
        return iReturnCode;
    }


}
